package z_mxsTest.testTree;

import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class TreeMenuExample {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("AutoSAR 工具 - 树形菜单 + 主工作区");
        
        // 使用 GridLayout，分成左右两列
        shell.setLayout(new GridLayout(2, false));

        // ====== 左侧面板：树形菜单 ======
        Composite leftPanel = new Composite(shell, SWT.BORDER);
        leftPanel.setLayout(new FillLayout());
        leftPanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true));

        // 创建 Tree 控件
        final Tree tree = new Tree(leftPanel, SWT.BORDER | SWT.V_SCROLL);

        // 添加一级节点
        TreeItem configItem = new TreeItem(tree, SWT.NONE);
        configItem.setText("配置管理");

        // 添加二级节点
        TreeItem basicConfig = new TreeItem(configItem, SWT.NONE);
        basicConfig.setText("基本配置");

        TreeItem advancedConfig = new TreeItem(configItem, SWT.NONE);
        advancedConfig.setText("高级配置");

        // 用户管理
        TreeItem userItem = new TreeItem(tree, SWT.NONE);
        userItem.setText("用户管理");

        TreeItem userList = new TreeItem(userItem, SWT.NONE);
        userList.setText("用户列表");

        TreeItem roles = new TreeItem(userItem, SWT.NONE);
        roles.setText("角色权限");

        // 日志查看
        TreeItem logsItem = new TreeItem(tree, SWT.NONE);
        logsItem.setText("日志查看");

        // 默认展开
        configItem.setExpanded(true);
        userItem.setExpanded(true);

        // ====== 右侧主工作区 ======
        final Composite workArea = new Composite(shell, SWT.BORDER);
        workArea.setLayout(new FillLayout());
        workArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        // 初始内容（可选）
        Label initialLabel = new Label(workArea, SWT.CENTER);
        initialLabel.setText("请选择左侧菜单项以加载内容");

        // ====== 点击事件监听器 ======
        tree.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                // 清除旧内容
                for (Control c : workArea.getChildren()) {
                    c.dispose();
                }

                // 获取选中的项
                TreeItem selectedItem = tree.getSelection()[0];
                String selectedText = selectedItem.getText();

                // === 下面可以根据不同选项创建不同内容 ===
                if ("基本配置".equals(selectedText)) {
                    Label label = new Label(workArea, SWT.CENTER);
                    label.setText("这里是「基本配置」面板");
                    
                } else if ("高级配置".equals(selectedText)) {
                    Label label = new Label(workArea, SWT.CENTER);
                    label.setText("这里是「高级配置」面板");
                    
                } else if ("用户列表".equals(selectedText)) {
                    Label label = new Label(workArea, SWT.CENTER);
                    label.setText("这里是「用户列表」面板");
                    
                } else if ("角色权限".equals(selectedText)) {
                    Label label = new Label(workArea, SWT.CENTER);
                    label.setText("这里是「角色权限」面板");
                    
                } else if ("日志查看".equals(selectedText)) {
                    Label label = new Label(workArea, SWT.CENTER);
                    label.setText("这里是「日志查看」面板");
                    
                } else {
                    Label label = new Label(workArea, SWT.CENTER);
                    label.setText("未知菜单项：" + selectedText);
                }

                // 重新布局
                workArea.layout(true, true);
            }
        });

        shell.setSize(1000, 600);
        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
}
